<?php

App::uses('AppController', 'Controller');

class CategoriesController extends AppController {

	var $name = 'Categories';
	var $uses = array('Category', 'Image');



	/**
	 * Danh Sach menu con
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function lists($parent_id) {
		$this->set('items', $this->Category->getAll( (int)$parent_id));
		$this->set('cate_id', $parent_id);
		$this->set('category', $this->Category->read(array('id','name', 'desc'), (int)$parent_id));
		$this->set('images', $this->Image->getAll( 'Category-'.$parent_id));
		$this->set('als', 'Category');
	}

	/**
	 * Danh sanh list by parent
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function getListByParent($parent_id) {
		$this->_responseJson(array('cates' => $this->Category->getAll($parent_id)));
	}

	/**
	 * Danh sach san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_index() {
		$items = $this->Category->getAll();
		foreach($items as $k => $v) {
			$v['Category']['childs'] = $this->Category->getAll($v['Category']['id']);
			$items[$k] = $v;
		}

		$this->set('items', $items);
	}

	/**
	 * Add
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_add($parent_id = 0, $id = null)
	{
		if(!empty($this->request->data))
		{
			$data = &$this->request->data[$this->als];

			$this->Category->set($data);
			$errors = $this->Category->invalidFields();

			//Save
			if(empty($errors)) {

				// kiem tra edit
				if( empty($data['id']) ) {
					unset($data['id']);
				} else {
					$data['id'] = (int)$data['id'];
				}

				$data['parent_id'] = isset($data['parent_id']) ? (int)$data['parent_id'] : 0;

				$data['status'] = 4;

				if($this->Category->save($data, false)) {
					// Xoa hinh anh cu
					if(!empty($data['image_del'])) {
						$this->Common->_delImageByListId($data['image_del'], $this->Image);
					}

					// Luu hinh anh
					if(!empty($data['images'])) {
						$image = array( 'target_id' => 'Category-'.$this->Category->id );
						foreach($data['images'] as $img) {
							if(!empty($img['name'])) {
								$image['image'] = $this->_upload($img);

								if(!empty($image['image'])) {
									$this->Image->create();
									$this->Image->save($image);
								}
							}
						}
					}

					$this->Session->setFlash('Lưu danh mục thành công.', 'success');
					$this->redirect('/admin' );
				} else {
					$this->Session->setFlash('Lưu danh mục thất bại. Vui lòng xem lại thông tin bên dưới', 'error');
				}
			} else {
				$this->Session->setFlash('Lưu danh mục thất bại.', 'error');
			}
		}

		// Truong hop show edit
		if( !empty( $id )) {
			$info = $this->Category->getInfoById($id);
			if(!empty($info)) {
				$this->request->data = $info;
				$this->set('images', $this->Image->getAll('Category-'.$id));
			}
		}

		$this->set('category_lists', $this->Category->getList());
		$this->set('cate_id', $parent_id);
	}

	/**
	 * delete san pham
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_delete($parent_id = 0, $id = null){
		if(!empty($id)) {
			$id = (int)$id;
			if( $this->Category->delete($id)) {

				App::uses('Product', 'Model');
				$P = new Product();

				// Xoa product
				$products = $P->getAll($id, array('id'));
				if(!empty($products)) {
					foreach($products as $p) {
						$P->delete($p['Product']['id']);

						// Xoa images
						$this->Common->_delImageByTargetId( $p['Product']['id'] );
					}
				}
				$this->Session->setFlash('Xóa danh mục thành công.', 'success');
			} else {
				$this->Session->setFlash('Xóa danh mục thất bại. Vui lòng thử lại sao', 'error');
			}
		} else {
			$this->Session->setFlash('Không tìm thấy danh mục. Vui lòng thử lại sao', 'error');
		}
		$this->redirect('/admin/categories/index/'.$parent_id);
	}

		/**
	 * Change Sort
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_changeSort() {
		if(!empty($this->request->data['id']) && !empty($this->request->data['sort'])) {
			$this->Category->id = (int)$this->request->data['id'];

			if($this->Category->saveField('sort', (int)$this->request->data['sort']))
				$this->_responseJson(array('code' => 'success'));
			else
				$this->_responseJson(array('code' => 'danger'));
		}
		$this->_responseJson(array('code' => 'danger'));
	}

	/**
	 * View Image
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_images($parent_id, $id) {
		$info = $this->Category->read(array('id', 'name'), (int)$id);
		$this->set('images', $this->Image->getAll('Category-'.$id));
		$this->set('category', $info['Category']);
		$this->set('cate_id', $parent_id);
	}


}
